import { defineStore } from "pinia"
import userUser from "@/stores/user"
const useCounter = defineStore("counter", {
    state: () => ({
        count: 99,
        friend: [
            { id: 1, name: "小七" },
            { id: 2, name: "小八" },
            { id: 3, name: "小九" }
        ]
    }),
    getters: {
        // 1.基本使用
        doubleCount() {
            return this.count * 2
        },
        // 2.使用其他getters
        doubleCountAdd() {
            return this.doubleCount + 1
        },
        // 3.返回函数
        friendMessage(state) {
            // 高级函数写法
            return friendId => {
                return state.friend.find(item => item.id === friendId)
            }
            // for循环遍历写法
            // return function (id) {
            //     for (let i = 0; i < state.friend.length; i++) {
            //         const friends = state.friend[i]
            //         if (friends.id === id) {
            //             return friends
            //         }
            //     }
            // }
        },
        // 4.拿到其他store中的数据
        getUseName(state) {
            const userName = userUser()
            return `name:${userName.name}-count:${state.count}`
        }
    }
})
export default useCounter